home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 February (DVD) / PCWorld_2008-02_DVD.iso / v cisle / PHP / PHP.exe / xampp-win32-1.6.5-installer.exe / phpMyAdmin / libraries / Config.class.php < prev    next >
Encoding:
PHP Script  |  2007-12-20  |  33.1 KB  |  1,037 lines

  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4.  *
  5.  *
  6.  * @version $Id: Config.class.php 10967 2007-12-08 12:46:36Z lem9 $
  7.  */
  8.  
  9. /**
  10.  * Configuration class
  11.  *
  12.  */
  13. class PMA_Config
  14. {
  15.     /**
  16.      * @var string  default config source
  17.      */
  18.     var $default_source = './libraries/config.default.php';
  19.  
  20.     /**
  21.      * @var array   configuration settings
  22.      */
  23.     var $settings = array();
  24.  
  25.     /**
  26.      * @var string  config source
  27.      */
  28.     var $source = '';
  29.  
  30.     /**
  31.      * @var int     source modification time
  32.      */
  33.     var $source_mtime = 0;
  34.     var $default_source_mtime = 0;
  35.     var $set_mtime = 0;
  36.  
  37.     /**
  38.      * @var boolean
  39.      */
  40.     var $error_config_file = false;
  41.  
  42.     /**
  43.      * @var boolean
  44.      */
  45.     var $error_config_default_file = false;
  46.  
  47.     /**
  48.      * @var boolean
  49.      */
  50.     var $error_pma_uri = false;
  51.  
  52.     /**
  53.      * @var array
  54.      */
  55.     var $default_server = array();
  56.  
  57.     /**
  58.      * @var boolean wether init is done or mot
  59.      * set this to false to force some initial checks
  60.      * like checking for required functions
  61.      */
  62.     var $done = false;
  63.  
  64.     /**
  65.      * constructor
  66.      *
  67.      * @param   string  source to read config from
  68.      */
  69.     function __construct($source = null)
  70.     {
  71.         $this->settings = array();
  72.  
  73.         // functions need to refresh in case of config file changed goes in
  74.         // PMA_Config::load()
  75.         $this->load($source);
  76.  
  77.         // other settings, independant from config file, comes in
  78.         $this->checkSystem();
  79.  
  80.         $this->checkIsHttps();
  81.     }
  82.  
  83.     /**
  84.      * sets system and application settings
  85.      */
  86.     function checkSystem()
  87.     {
  88.         $this->set('PMA_VERSION', '2.11.3');
  89.         /**
  90.          * @deprecated
  91.          */
  92.         $this->set('PMA_THEME_VERSION', 2);
  93.         /**
  94.          * @deprecated
  95.          */
  96.         $this->set('PMA_THEME_GENERATION', 2);
  97.  
  98.         $this->checkPhpVersion();
  99.         $this->checkWebServerOs();
  100.         $this->checkWebServer();
  101.         $this->checkGd2();
  102.         $this->checkClient();
  103.         $this->checkUpload();
  104.         $this->checkUploadSize();
  105.         $this->checkOutputCompression();
  106.     }
  107.  
  108.     /**
  109.      * wether to use gzip output compression or not
  110.      */
  111.     function checkOutputCompression()
  112.     {
  113.         // If zlib output compression is set in the php configuration file, no
  114.         // output buffering should be run
  115.         if (@ini_get('zlib.output_compression')) {
  116.             $this->set('OBGzip', false);
  117.         }
  118.  
  119.         // disable output-buffering (if set to 'auto') for IE6, else enable it.
  120.         if (strtolower($this->get('OBGzip')) == 'auto') {
  121.             if ($this->get('PMA_USR_BROWSER_AGENT') == 'IE'
  122.               && $this->get('PMA_USR_BROWSER_VER') >= 6
  123.               && $this->get('PMA_USR_BROWSER_VER') < 7) {
  124.                 $this->set('OBGzip', false);
  125.             } else {
  126.                 $this->set('OBGzip', true);
  127.             }
  128.         }
  129.     }
  130.  
  131.     /**
  132.      * Determines platform (OS), browser and version of the user
  133.      * Based on a phpBuilder article:
  134.      * @see http://www.phpbuilder.net/columns/tim20000821.php
  135.      */
  136.     function checkClient()
  137.     {
  138.         if (PMA_getenv('HTTP_USER_AGENT')) {
  139.             $HTTP_USER_AGENT = PMA_getenv('HTTP_USER_AGENT');
  140.         } elseif (!isset($HTTP_USER_AGENT)) {
  141.             $HTTP_USER_AGENT = '';
  142.         }
  143.  
  144.         // 1. Platform
  145.         if (strstr($HTTP_USER_AGENT, 'Win')) {
  146.             $this->set('PMA_USR_OS', 'Win');
  147.         } elseif (strstr($HTTP_USER_AGENT, 'Mac')) {
  148.             $this->set('PMA_USR_OS', 'Mac');
  149.         } elseif (strstr($HTTP_USER_AGENT, 'Linux')) {
  150.             $this->set('PMA_USR_OS', 'Linux');
  151.         } elseif (strstr($HTTP_USER_AGENT, 'Unix')) {
  152.             $this->set('PMA_USR_OS', 'Unix');
  153.         } elseif (strstr($HTTP_USER_AGENT, 'OS/2')) {
  154.             $this->set('PMA_USR_OS', 'OS/2');
  155.         } else {
  156.             $this->set('PMA_USR_OS', 'Other');
  157.         }
  158.  
  159.         // 2. browser and version
  160.         // (must check everything else before Mozilla)
  161.  
  162.         if (preg_match('@Opera(/| )([0-9].[0-9]{1,2})@', $HTTP_USER_AGENT, $log_version)) {
  163.             $this->set('PMA_USR_BROWSER_VER', $log_version[2]);
  164.             $this->set('PMA_USR_BROWSER_AGENT', 'OPERA');
  165.         } elseif (preg_match('@MSIE ([0-9].[0-9]{1,2})@', $HTTP_USER_AGENT, $log_version)) {
  166.             $this->set('PMA_USR_BROWSER_VER', $log_version[1]);
  167.             $this->set('PMA_USR_BROWSER_AGENT', 'IE');
  168.         } elseif (preg_match('@OmniWeb/([0-9].[0-9]{1,2})@', $HTTP_USER_AGENT, $log_version)) {
  169.             $this->set('PMA_USR_BROWSER_VER', $log_version[1]);
  170.             $this->set('PMA_USR_BROWSER_AGENT', 'OMNIWEB');
  171.         //} elseif (ereg('Konqueror/([0-9].[0-9]{1,2})', $HTTP_USER_AGENT, $log_version)) {
  172.         // Konqueror 2.2.2 says Konqueror/2.2.2
  173.         // Konqueror 3.0.3 says Konqueror/3
  174.         } elseif (preg_match('@(Konqueror/)(.*)(;)@', $HTTP_USER_AGENT, $log_version)) {
  175.             $this->set('PMA_USR_BROWSER_VER', $log_version[2]);
  176.             $this->set('PMA_USR_BROWSER_AGENT', 'KONQUEROR');
  177.         } elseif (preg_match('@Mozilla/([0-9].[0-9]{1,2})@', $HTTP_USER_AGENT, $log_version)
  178.                    && preg_match('@Safari/([0-9]*)@', $HTTP_USER_AGENT, $log_version2)) {
  179.             $this->set('PMA_USR_BROWSER_VER', $log_version[1] . '.' . $log_version2[1]);
  180.             $this->set('PMA_USR_BROWSER_AGENT', 'SAFARI');
  181.         } elseif (preg_match('@Mozilla/([0-9].[0-9]{1,2})@', $HTTP_USER_AGENT, $log_version)) {
  182.             $this->set('PMA_USR_BROWSER_VER', $log_version[1]);
  183.             $this->set('PMA_USR_BROWSER_AGENT', 'MOZILLA');
  184.         } else {
  185.             $this->set('PMA_USR_BROWSER_VER', 0);
  186.             $this->set('PMA_USR_BROWSER_AGENT', 'OTHER');
  187.         }
  188.     }
  189.  
  190.     /**
  191.      * Whether GD2 is present
  192.      */
  193.     function checkGd2()
  194.     {
  195.         if ($this->get('GD2Available') == 'yes') {
  196.             $this->set('PMA_IS_GD2', 1);
  197.         } elseif ($this->get('GD2Available') == 'no') {
  198.             $this->set('PMA_IS_GD2', 0);
  199.         } else {
  200.             if (!@extension_loaded('gd')) {
  201.                 PMA_dl('gd');
  202.             }
  203.             if (!@function_exists('imagecreatetruecolor')) {
  204.                 $this->set('PMA_IS_GD2', 0);
  205.             } else {
  206.                 if (@function_exists('gd_info')) {
  207.                     $gd_nfo = gd_info();
  208.                     if (strstr($gd_nfo["GD Version"], '2.')) {
  209.                         $this->set('PMA_IS_GD2', 1);
  210.                     } else {
  211.                         $this->set('PMA_IS_GD2', 0);
  212.                     }
  213.                 } else {
  214.                     /* We must do hard way... */
  215.                     ob_start();
  216.                     phpinfo(INFO_MODULES); /* Only modules */
  217.                     $a = strip_tags(ob_get_contents());
  218.                     ob_end_clean();
  219.                     /* Get GD version string from phpinfo output */
  220.                     if (preg_match('@GD Version[[:space:]]*\(.*\)@', $a, $v)) {
  221.                         if (strstr($v, '2.')) {
  222.                             $this->set('PMA_IS_GD2', 1);
  223.                         } else {
  224.                             $this->set('PMA_IS_GD2', 0);
  225.                         }
  226.                     } else {
  227.                         $this->set('PMA_IS_GD2', 0);
  228.                     }
  229.                 }
  230.             }
  231.         }
  232.     }
  233.  
  234.     /**
  235.      * Whether the Web server php is running on is IIS
  236.      */
  237.     function checkWebServer()
  238.     {
  239.         if (PMA_getenv('SERVER_SOFTWARE')
  240.           // some versions return Microsoft-IIS, some Microsoft/IIS
  241.           // we could use a preg_match() but it's slower
  242.           && stristr(PMA_getenv('SERVER_SOFTWARE'), 'Microsoft')
  243.           && stristr(PMA_getenv('SERVER_SOFTWARE'), 'IIS')) {
  244.             $this->set('PMA_IS_IIS', 1);
  245.         } else {
  246.             $this->set('PMA_IS_IIS', 0);
  247.         }
  248.     }
  249.  
  250.     /**
  251.      * Whether the os php is running on is windows or not
  252.      */
  253.     function checkWebServerOs()
  254.     {
  255.         // Default to Unix or Equiv
  256.         $this->set('PMA_IS_WINDOWS', 0);
  257.         // If PHP_OS is defined then continue
  258.         if (defined('PHP_OS')) {
  259.             if (stristr(PHP_OS, 'win')) {
  260.                 // Is it some version of Windows
  261.                 $this->set('PMA_IS_WINDOWS', 1);
  262.             } elseif (stristr(PHP_OS, 'OS/2')) {
  263.                 // Is it OS/2 (No file permissions like Windows)
  264.                 $this->set('PMA_IS_WINDOWS', 1);
  265.             }
  266.         }
  267.     }
  268.  
  269.     /**
  270.      * detects PHP version
  271.      */
  272.     function checkPhpVersion()
  273.     {
  274.         $match = array();
  275.         if (! preg_match('@([0-9]{1,2}).([0-9]{1,2}).([0-9]{1,2})@',
  276.                 phpversion(), $match)) {
  277.             $result = preg_match('@([0-9]{1,2}).([0-9]{1,2})@',
  278.                 phpversion(), $match);
  279.         }
  280.         if (isset($match) && ! empty($match[1])) {
  281.             if (! isset($match[2])) {
  282.                 $match[2] = 0;
  283.             }
  284.             if (! isset($match[3])) {
  285.                 $match[3] = 0;
  286.             }
  287.             $this->set('PMA_PHP_INT_VERSION',
  288.                 (int) sprintf('%d%02d%02d', $match[1], $match[2], $match[3]));
  289.         } else {
  290.             $this->set('PMA_PHP_INT_VERSION', 0);
  291.         }
  292.         $this->set('PMA_PHP_STR_VERSION', phpversion());
  293.     }
  294.  
  295.     /**
  296.      * re-init object after loading from session file
  297.      * checks config file for changes and relaods if neccessary
  298.      */
  299.     function __wakeup()
  300.     {
  301.         if (! $this->checkConfigSource()
  302.           || $this->source_mtime !== filemtime($this->getSource())
  303.           || $this->default_source_mtime !== filemtime($this->default_source)
  304.           || $this->error_config_file
  305.           || $this->error_config_default_file) {
  306.             $this->settings = array();
  307.             $this->load();
  308.             $this->checkSystem();
  309.         }
  310.  
  311.         // check for https needs to be done everytime,
  312.         // as https and http uses same session so this info can not be stored
  313.         // in session
  314.         $this->checkIsHttps();
  315.  
  316.         $this->checkCollationConnection();
  317.         $this->checkFontsize();
  318.     }
  319.  
  320.     /**
  321.      * loads default values from default source
  322.      *
  323.      * @uses    file_exists()
  324.      * @uses    $this->default_source
  325.      * @uses    $this->error_config_default_file
  326.      * @uses    $this->settings
  327.      * @return  boolean     success
  328.      */
  329.     function loadDefaults()
  330.     {
  331.         $cfg = array();
  332.         if (! file_exists($this->default_source)) {
  333.             $this->error_config_default_file = true;
  334.             return false;
  335.         }
  336.         include $this->default_source;
  337.  
  338.         $this->default_source_mtime = filemtime($this->default_source);
  339.  
  340.         $this->default_server = $cfg['Servers'][1];
  341.         unset($cfg['Servers']);
  342.  
  343.         $this->settings = PMA_array_merge_recursive($this->settings, $cfg);
  344.  
  345.         $this->error_config_default_file = false;
  346.  
  347.         return true;
  348.     }
  349.  
  350.     /**
  351.      * loads configuration from $source, usally the config file
  352.      * should be called on object creation and from __wakeup if config file
  353.      * has changed
  354.      *
  355.      * @param   string $source  config file
  356.      */
  357.     function load($source = null)
  358.     {
  359.         $this->loadDefaults();
  360.  
  361.         if (null !== $source) {
  362.             $this->setSource($source);
  363.         }
  364.  
  365.         if (! $this->checkConfigSource()) {
  366.             return false;
  367.         }
  368.  
  369.         $cfg = array();
  370.  
  371.         /**
  372.          * Parses the configuration file
  373.          */
  374.         $old_error_reporting = error_reporting(0);
  375.         if (function_exists('file_get_contents')) {
  376.             $eval_result =
  377.                 eval('?>' . trim(file_get_contents($this->getSource())));
  378.         } else {
  379.             $eval_result =
  380.                 eval('?>' . trim(implode("\n", file($this->getSource()))));
  381.         }
  382.         error_reporting($old_error_reporting);
  383.  
  384.         if ($eval_result === false) {
  385.             $this->error_config_file = true;
  386.         } else  {
  387.             $this->error_config_file = false;
  388.             $this->source_mtime = filemtime($this->getSource());
  389.         }
  390.  
  391.         /**
  392.          * Backward compatibility code
  393.          */
  394.         if (!empty($cfg['DefaultTabTable'])) {
  395.             $cfg['DefaultTabTable'] = str_replace('_properties', '', str_replace('tbl_properties.php', 'tbl_sql.php', $cfg['DefaultTabTable']));
  396.         }
  397.         if (!empty($cfg['DefaultTabDatabase'])) {
  398.             $cfg['DefaultTabDatabase'] = str_replace('_details', '', str_replace('db_details.php', 'db_sql.php', $cfg['DefaultTabDatabase']));
  399.         }
  400.  
  401.         $this->checkFontsize();
  402.         //$this->checkPmaAbsoluteUri();
  403.         $this->settings = PMA_array_merge_recursive($this->settings, $cfg);
  404.  
  405.         // Handling of the collation must be done after merging of $cfg
  406.         // (from config.inc.php) so that $cfg['DefaultConnectionCollation']
  407.         // can have an effect. Note that the presence of collation
  408.         // information in a cookie has priority over what is defined
  409.         // in the default or user's config files.
  410.         /**
  411.          * @todo check validity of $_COOKIE['pma_collation_connection']
  412.          */
  413.         if (! empty($_COOKIE['pma_collation_connection'])) {
  414.             $this->set('collation_connection',
  415.                 strip_tags($_COOKIE['pma_collation_connection']));
  416.         } else {
  417.             $this->set('collation_connection',
  418.                 $this->get('DefaultConnectionCollation'));
  419.         }
  420.         // Now, a collation information could come from REQUEST
  421.         // (an example of this: the collation selector in main.php)
  422.         // so the following handles the setting of collation_connection
  423.         // and later, in common.inc.php, the cookie will be set
  424.         // according to this.
  425.         $this->checkCollationConnection();
  426.  
  427.         return true;
  428.     }
  429.  
  430.     /**
  431.      * set source
  432.      * @param   string  $source
  433.      */
  434.     function setSource($source)
  435.     {
  436.         $this->source = trim($source);
  437.     }
  438.  
  439.     /**
  440.      * checks if the config folder still exists and terminates app if true
  441.      */
  442.     function checkConfigFolder()
  443.     {
  444.         // Refuse to work while there still might be some world writable dir:
  445.         if (is_dir('./config')) {
  446.             die('Remove "./config" directory before using phpMyAdmin!');
  447.         }
  448.     }
  449.  
  450.     /**
  451.      * check config source
  452.      *
  453.      * @return  boolean wether source is valid or not
  454.      */
  455.     function checkConfigSource()
  456.     {
  457.         if (! $this->getSource()) {
  458.             // no configuration file set at all
  459.             return false;
  460.         }
  461.  
  462.         if (! file_exists($this->getSource())) {
  463.             // do not trigger error here
  464.             // https://sf.net/tracker/?func=detail&aid=1370269&group_id=23067&atid=377408
  465.             /*
  466.             trigger_error(
  467.                 'phpMyAdmin-ERROR: unkown configuration source: ' . $source,
  468.                 E_USER_WARNING);
  469.             */
  470.             $this->source_mtime = 0;
  471.             return false;
  472.         }
  473.  
  474.         if (! is_readable($this->getSource())) {
  475.             $this->source_mtime = 0;
  476.             die('Existing configuration file (' . $this->getSource() . ') is not readable.');
  477.         }
  478.  
  479.         // Check for permissions (on platforms that support it):
  480.         $perms = @fileperms($this->getSource());
  481.         if (!($perms === false) && ($perms & 2)) {
  482.             // This check is normally done after loading configuration
  483.             $this->checkWebServerOs();
  484.             if ($this->get('PMA_IS_WINDOWS') == 0) {
  485.                 $this->source_mtime = 0;
  486.                 die('Wrong permissions on configuration file, should not be world writable!');
  487.             }
  488.         }
  489.  
  490.         return true;
  491.     }
  492.  
  493.     /**
  494.      * returns specific config setting
  495.      * @param   string  $setting
  496.      * @return  mixed   value
  497.      */
  498.     function get($setting)
  499.     {
  500.         if (isset($this->settings[$setting])) {
  501.             return $this->settings[$setting];
  502.         }
  503.         return null;
  504.     }
  505.  
  506.     /**
  507.      * sets configuration variable
  508.      *
  509.      * @uses    $this->settings
  510.      * @param   string  $setting    configuration option
  511.      * @param   string  $value      new value for configuration option
  512.      */
  513.     function set($setting, $value)
  514.     {
  515.         if (!isset($this->settings[$setting]) || $this->settings[$setting] != $value) {
  516.             $this->settings[$setting] = $value;
  517.             $this->set_mtime = time();
  518.         }
  519.     }
  520.  
  521.     /**
  522.      * returns source for current config
  523.      * @return  string  config source
  524.      */
  525.     function getSource()
  526.     {
  527.         return $this->source;
  528.     }
  529.  
  530.     /**
  531.      * old PHP 4 style constructor
  532.      *
  533.      * @deprecated
  534.      */
  535.     function PMA_Config($source = null)
  536.     {
  537.         $this->__construct($source);
  538.     }
  539.  
  540.     /**
  541.      * returns a unique value to force a CSS reload if either the config
  542.      * or the theme changes
  543.      * @return  int  Unix timestamp
  544.      */
  545.     function getMtime()
  546.     {
  547.         return intval($_SESSION['PMA_Config']->get('fontsize')) + ($this->source_mtime + $this->default_source_mtime + $_SESSION['PMA_Theme']->mtime_info);
  548.     }
  549.  
  550.     /**
  551.      * $cfg['PmaAbsoluteUri'] is a required directive else cookies won't be
  552.      * set properly and, depending on browsers, inserting or updating a
  553.      * record might fail
  554.      */
  555.     function checkPmaAbsoluteUri()
  556.     {
  557.         // Setup a default value to let the people and lazy syadmins work anyway,
  558.         // they'll get an error if the autodetect code doesn't work
  559.         $pma_absolute_uri = $this->get('PmaAbsoluteUri');
  560.         $is_https = $this->get('is_https');
  561.  
  562.         if (strlen($pma_absolute_uri) < 5
  563.             // needed to catch http/https switch
  564.             || ($is_https && substr($pma_absolute_uri, 0, 6) != 'https:')
  565.             || (!$is_https && substr($pma_absolute_uri, 0, 5) != 'http:')
  566.         ) {
  567.             $url = array();
  568.  
  569.             // At first we try to parse REQUEST_URI, it might contain full URL
  570.             if (PMA_getenv('REQUEST_URI')) {
  571.                 $url = @parse_url(PMA_getenv('REQUEST_URI')); // produces E_WARNING if it cannot get parsed, e.g. '/foobar:/'
  572.                 if ($url === false) {
  573.                     $url = array('path' => $_SERVER['REQUEST_URI']);
  574.                 }
  575.             }
  576.  
  577.             // If we don't have scheme, we didn't have full URL so we need to
  578.             // dig deeper
  579.             if (empty($url['scheme'])) {
  580.                 // Scheme
  581.                 if (PMA_getenv('HTTP_SCHEME')) {
  582.                     $url['scheme'] = PMA_getenv('HTTP_SCHEME');
  583.                 } else {
  584.                     $url['scheme'] =
  585.                         PMA_getenv('HTTPS') && strtolower(PMA_getenv('HTTPS')) != 'off'
  586.                             ? 'https'
  587.                             : 'http';
  588.                 }
  589.  
  590.                 // Host and port
  591.                 if (PMA_getenv('HTTP_HOST')) {
  592.                     if (strpos(PMA_getenv('HTTP_HOST'), ':') !== false) {
  593.                         list($url['host'], $url['port']) =
  594.                             explode(':', PMA_getenv('HTTP_HOST'));
  595.                     } else {
  596.                         $url['host'] = PMA_getenv('HTTP_HOST');
  597.                     }
  598.                 } elseif (PMA_getenv('SERVER_NAME')) {
  599.                     $url['host'] = PMA_getenv('SERVER_NAME');
  600.                 } else {
  601.                     $this->error_pma_uri = true;
  602.                     return false;
  603.                 }
  604.  
  605.                 // If we didn't set port yet...
  606.                 if (empty($url['port']) && PMA_getenv('SERVER_PORT')) {
  607.                     $url['port'] = PMA_getenv('SERVER_PORT');
  608.                 }
  609.  
  610.                 // And finally the path could be already set from REQUEST_URI
  611.                 if (empty($url['path'])) {
  612.                     if (PMA_getenv('PATH_INFO')) {
  613.                         $path = parse_url(PMA_getenv('PATH_INFO'));
  614.                     } else {
  615.                         // PHP_SELF in CGI often points to cgi executable, so use it
  616.                         // as last choice
  617.                         $path = parse_url(PMA_getenv('PHP_SELF'));
  618.                     }
  619.                     $url['path'] = $path['path'];
  620.                 }
  621.             }
  622.  
  623.             // Make url from parts we have
  624.             $pma_absolute_uri = $url['scheme'] . '://';
  625.             // Was there user information?
  626.             if (!empty($url['user'])) {
  627.                 $pma_absolute_uri .= $url['user'];
  628.                 if (!empty($url['pass'])) {
  629.                     $pma_absolute_uri .= ':' . $url['pass'];
  630.                 }
  631.                 $pma_absolute_uri .= '@';
  632.             }
  633.             // Add hostname
  634.             $pma_absolute_uri .= $url['host'];
  635.             // Add port, if it not the default one
  636.             if (! empty($url['port'])
  637.               && (($url['scheme'] == 'http' && $url['port'] != 80)
  638.                 || ($url['scheme'] == 'https' && $url['port'] != 443))) {
  639.                 $pma_absolute_uri .= ':' . $url['port'];
  640.             }
  641.             // And finally path, without script name, the 'a' is there not to
  642.             // strip our directory, when path is only /pmadir/ without filename.
  643.             // Backslashes returned by Windows have to be changed.
  644.             // Only replace backslashes by forward slashes if on Windows,
  645.             // as the backslash could be valid on a non-Windows system.
  646.             if ($this->get('PMA_IS_WINDOWS') == 1) {
  647.                 $path = str_replace("\\", "/", dirname($url['path'] . 'a'));
  648.             } else {
  649.                 $path = dirname($url['path'] . 'a');
  650.             }
  651.  
  652.             // To work correctly within transformations overview:
  653.             if (defined('PMA_PATH_TO_BASEDIR') && PMA_PATH_TO_BASEDIR == '../../') {
  654.                 if ($this->get('PMA_IS_WINDOWS') == 1) {
  655.                     $path = str_replace("\\", "/", dirname(dirname($path)));
  656.                 } else {
  657.                     $path = dirname(dirname($path));
  658.                 }
  659.             }
  660.             // in vhost situations, there could be already an ending slash
  661.             if (substr($path, -1) != '/') {
  662.                 $path .= '/';
  663.             }
  664.             $pma_absolute_uri .= $path;
  665.  
  666.             // We used to display a warning if PmaAbsoluteUri wasn't set, but now
  667.             // the autodetect code works well enough that we don't display the
  668.             // warning at all. The user can still set PmaAbsoluteUri manually.
  669.             // See
  670.             // http://sf.net/tracker/?func=detail&aid=1257134&group_id=23067&atid=377411
  671.  
  672.         } else {
  673.             // The URI is specified, however users do often specify this
  674.             // wrongly, so we try to fix this.
  675.  
  676.             // Adds a trailing slash et the end of the phpMyAdmin uri if it
  677.             // does not exist.
  678.             if (substr($pma_absolute_uri, -1) != '/') {
  679.                 $pma_absolute_uri .= '/';
  680.             }
  681.  
  682.             // If URI doesn't start with http:// or https://, we will add
  683.             // this.
  684.             if (substr($pma_absolute_uri, 0, 7) != 'http://'
  685.               && substr($pma_absolute_uri, 0, 8) != 'https://') {
  686.                 $pma_absolute_uri =
  687.                     (PMA_getenv('HTTPS') && strtolower(PMA_getenv('HTTPS')) != 'off'
  688.                         ? 'https'
  689.                         : 'http')
  690.                     . ':' . (substr($pma_absolute_uri, 0, 2) == '//' ? '' : '//')
  691.                     . $pma_absolute_uri;
  692.             }
  693.         }
  694.         $this->set('PmaAbsoluteUri', $pma_absolute_uri);
  695.     }
  696.  
  697.     /**
  698.      * check selected collation_connection
  699.      * @todo check validity of $_REQUEST['collation_connection']
  700.      */
  701.     function checkCollationConnection()
  702.     {
  703.         // (could be improved by executing it after the MySQL connection only if
  704.         //  PMA_MYSQL_INT_VERSION >= 40100)
  705.         if (! empty($_REQUEST['collation_connection'])) {
  706.             $this->set('collation_connection',
  707.                 strip_tags($_REQUEST['collation_connection']));
  708.         }
  709.     }
  710.  
  711.     /**
  712.      * checks for font size configuration, and sets font size as requested by user
  713.      *
  714.      * @uses    $_GET
  715.      * @uses    $_POST
  716.      * @uses    $_COOKIE
  717.      * @uses    preg_match()
  718.      * @uses    function_exists()
  719.      * @uses    PMA_Config::set()
  720.      * @uses    PMA_Config::get()
  721.      * @uses    PMA_setCookie()
  722.      */
  723.     function checkFontsize()
  724.     {
  725.         $new_fontsize = '';
  726.  
  727.         if (isset($_GET['fontsize'])) {
  728.             $new_fontsize = $_GET['fontsize'];
  729.         } elseif (isset($_POST['fontsize'])) {
  730.             $new_fontsize = $_POST['fontsize'];
  731.         } elseif (isset($_COOKIE['pma_fontsize'])) {
  732.             $new_fontsize = $_COOKIE['pma_fontsize'];
  733.         }
  734.  
  735.         if (preg_match('/^[0-9.]+(px|em|pt|\%)$/', $new_fontsize)) {
  736.             $this->set('fontsize', $new_fontsize);
  737.         } elseif (! $this->get('fontsize')) {
  738.              // 80% would correspond to the default browser font size
  739.              // of 16, but use 82% to help read the monoface font
  740.             $this->set('fontsize', '82%');
  741.         }
  742.  
  743.         if (function_exists('PMA_setCookie')) {
  744.             PMA_setCookie('pma_fontsize', $this->get('fontsize'), '82%');
  745.         }
  746.     }
  747.  
  748.     /**
  749.      * checks if upload is enabled
  750.      *
  751.      */
  752.     function checkUpload()
  753.     {
  754.         if (ini_get('file_uploads')) {
  755.             $this->set('enable_upload', true);
  756.             // if set "php_admin_value file_uploads Off" in httpd.conf
  757.             // ini_get() also returns the string "Off" in this case:
  758.             if ('off' == strtolower(ini_get('file_uploads'))) {
  759.                 $this->set('enable_upload', false);
  760.             }
  761.          } else {
  762.             $this->set('enable_upload', false);
  763.          }
  764.     }
  765.  
  766.     /**
  767.      * Maximum upload size as limited by PHP
  768.      * Used with permission from Moodle (http://moodle.org) by Martin Dougiamas
  769.      *
  770.      * this section generates $max_upload_size in bytes
  771.      */
  772.     function checkUploadSize()
  773.     {
  774.         if (! $filesize = ini_get('upload_max_filesize')) {
  775.             $filesize = "5M";
  776.         }
  777.  
  778.         if ($postsize = ini_get('post_max_size')) {
  779.             $this->set('max_upload_size',
  780.                 min(PMA_get_real_size($filesize), PMA_get_real_size($postsize)));
  781.         } else {
  782.             $this->set('max_upload_size', PMA_get_real_size($filesize));
  783.         }
  784.     }
  785.  
  786.     /**
  787.      * check for https
  788.      */
  789.     function checkIsHttps()
  790.     {
  791.         $this->set('is_https', PMA_Config::isHttps());
  792.     }
  793.  
  794.     /**
  795.      * @static
  796.      */
  797.     function isHttps()
  798.     {
  799.         $is_https = false;
  800.  
  801.         $url = array();
  802.  
  803.         // At first we try to parse REQUEST_URI, it might contain full URL,
  804.         if (PMA_getenv('REQUEST_URI')) {
  805.             $url = @parse_url(PMA_getenv('REQUEST_URI')); // produces E_WARNING if it cannot get parsed, e.g. '/foobar:/'
  806.             if($url === false) {
  807.                 $url = array();
  808.             }
  809.         }
  810.  
  811.         // If we don't have scheme, we didn't have full URL so we need to
  812.         // dig deeper
  813.         if (empty($url['scheme'])) {
  814.             // Scheme
  815.             if (PMA_getenv('HTTP_SCHEME')) {
  816.                 $url['scheme'] = PMA_getenv('HTTP_SCHEME');
  817.             } else {
  818.                 $url['scheme'] =
  819.                     PMA_getenv('HTTPS') && strtolower(PMA_getenv('HTTPS')) != 'off'
  820.                         ? 'https'
  821.                         : 'http';
  822.             }
  823.         }
  824.  
  825.         if (isset($url['scheme'])
  826.           && $url['scheme'] == 'https') {
  827.             $is_https = true;
  828.         } else {
  829.             $is_https = false;
  830.         }
  831.  
  832.         return $is_https;
  833.     }
  834.  
  835.     /**
  836.      * detect correct cookie path
  837.      */
  838.     function checkCookiePath()
  839.     {
  840.         $this->set('cookie_path', PMA_Config::getCookiePath());
  841.     }
  842.  
  843.     /**
  844.      * @static
  845.      */
  846.     function getCookiePath()
  847.     {
  848.         static $cookie_path = null;
  849.  
  850.         if (null !== $cookie_path) {
  851.             return $cookie_path;
  852.         }
  853.  
  854.         $url = '';
  855.  
  856.         if (PMA_getenv('REQUEST_URI')) {
  857.             $url = PMA_getenv('REQUEST_URI');
  858.         }
  859.  
  860.         // If we don't have path
  861.         if (empty($url)) {
  862.             if (PMA_getenv('PATH_INFO')) {
  863.                 $url = PMA_getenv('PATH_INFO');
  864.             } elseif (PMA_getenv('PHP_SELF')) {
  865.                 // PHP_SELF in CGI often points to cgi executable, so use it
  866.                 // as last choice
  867.                 $url = PMA_getenv('PHP_SELF');
  868.             } elseif (PMA_getenv('SCRIPT_NAME')) {
  869.                 $url = PMA_getenv('PHP_SELF');
  870.             }
  871.         }
  872.  
  873.         $parsed_url = @parse_url($_SERVER['REQUEST_URI']); // produces E_WARNING if it cannot get parsed, e.g. '/foobar:/'
  874.         if ($parsed_url === false) {
  875.             $parsed_url = array('path' => $url);
  876.         }
  877.  
  878.         $cookie_path   = substr($parsed_url['path'], 0, strrpos($parsed_url['path'], '/'))  . '/';
  879.  
  880.         return $cookie_path;
  881.     }
  882.  
  883.     /**
  884.      * enables backward compatibility
  885.      */
  886.     function enableBc()
  887.     {
  888.         $GLOBALS['cfg']             =& $this->settings;
  889.         $GLOBALS['default_server']  =& $this->default_server;
  890.         $GLOBALS['collation_connection'] = $this->get('collation_connection');
  891.         $GLOBALS['is_upload']       = $this->get('enable_upload');
  892.         $GLOBALS['max_upload_size'] = $this->get('max_upload_size');
  893.         $GLOBALS['cookie_path']     = $this->get('cookie_path');
  894.         $GLOBALS['is_https']        = $this->get('is_https');
  895.  
  896.         $defines = array(
  897.             'PMA_VERSION',
  898.             'PMA_THEME_VERSION',
  899.             'PMA_THEME_GENERATION',
  900.             'PMA_PHP_STR_VERSION',
  901.             'PMA_PHP_INT_VERSION',
  902.             'PMA_IS_WINDOWS',
  903.             'PMA_IS_IIS',
  904.             'PMA_IS_GD2',
  905.             'PMA_USR_OS',
  906.             'PMA_USR_BROWSER_VER',
  907.             'PMA_USR_BROWSER_AGENT'
  908.             );
  909.  
  910.         foreach ($defines as $define) {
  911.             if (! defined($define)) {
  912.                 define($define, $this->get($define));
  913.             }
  914.         }
  915.     }
  916.  
  917.     /**
  918.      * @todo finish
  919.      */
  920.     function save() {}
  921.  
  922.     /**
  923.      * returns options for font size selection
  924.      *
  925.      * @uses    preg_replace()
  926.      * @uses    ksort()
  927.      * @static
  928.      * @param   string  $current_size   current selected font size with unit
  929.      * @return  array   selectable font sizes
  930.      */
  931.     function getFontsizeOptions($current_size = '82%')
  932.     {
  933.         $unit = preg_replace('/[0-9.]*/', '', $current_size);
  934.         $value = preg_replace('/[^0-9.]*/', '', $current_size);
  935.  
  936.         $factors = array();
  937.         $options = array();
  938.         $options["$value"] = $value . $unit;
  939.  
  940.         if ($unit === '%') {
  941.             $factors[] = 1;
  942.             $factors[] = 5;
  943.             $factors[] = 10;
  944.         } elseif ($unit === 'em') {
  945.             $factors[] = 0.05;
  946.             $factors[] = 0.2;
  947.             $factors[] = 1;
  948.         } elseif ($unit === 'pt') {
  949.             $factors[] = 0.5;
  950.             $factors[] = 2;
  951.         } elseif ($unit === 'px') {
  952.             $factors[] = 1;
  953.             $factors[] = 5;
  954.             $factors[] = 10;
  955.         } else {
  956.             //unknown font size unit
  957.             $factors[] = 0.05;
  958.             $factors[] = 0.2;
  959.             $factors[] = 1;
  960.             $factors[] = 5;
  961.             $factors[] = 10;
  962.         }
  963.  
  964.         foreach ($factors as $key => $factor) {
  965.             $option_inc = $value + $factor;
  966.             $option_dec = $value - $factor;
  967.             while (count($options) < 21) {
  968.                 $options["$option_inc"] = $option_inc . $unit;
  969.                 if ($option_dec > $factors[0]) {
  970.                     $options["$option_dec"] = $option_dec . $unit;
  971.                 }
  972.                 $option_inc += $factor;
  973.                 $option_dec -= $factor;
  974.                 if (isset($factors[$key + 1])
  975.                  && $option_inc >= $value + $factors[$key + 1]) {
  976.                     break;
  977.                 }
  978.             }
  979.         }
  980.         ksort($options);
  981.         return $options;
  982.     }
  983.  
  984.     /**
  985.      * returns html selectbox for font sizes
  986.      *
  987.      * @uses    $_SESSION['PMA_Config']
  988.      * @uses    PMA_Config::get()
  989.      * @uses    PMA_Config::getFontsizeOptions()
  990.      * @uses    $GLOBALS['strFontSize']
  991.      * @static
  992.      * @param   string  $current_size   currently slected font size with unit
  993.      * @return  string  html selectbox
  994.      */
  995.     function getFontsizeSelection()
  996.     {
  997.         $current_size = $_SESSION['PMA_Config']->get('fontsize');
  998.         $options = PMA_Config::getFontsizeOptions($current_size);
  999.  
  1000.         $return = '<label for="select_fontsize">' . $GLOBALS['strFontSize'] . ':</label>' . "\n";
  1001.         $return .= '<select name="fontsize" id="select_fontsize" onchange="this.form.submit();">' . "\n";
  1002.         foreach ($options as $option) {
  1003.             $return .= '<option value="' . $option . '"';
  1004.             if ($option == $current_size) {
  1005.                 $return .= ' selected="selected"';
  1006.             }
  1007.             $return .= '>' . $option . '</option>' . "\n";
  1008.         }
  1009.         $return .= '</select>';
  1010.  
  1011.         return $return;
  1012.     }
  1013.  
  1014.     /**
  1015.      * return complete font size selection form
  1016.      *
  1017.      * @uses    PMA_generate_common_hidden_inputs()
  1018.      * @uses    PMA_Config::getFontsizeSelection()
  1019.      * @uses    $GLOBALS['strGo']
  1020.      * @static
  1021.      * @param   string  $current_size   currently slected font size with unit
  1022.      * @return  string  html selectbox
  1023.      */
  1024.     function getFontsizeForm()
  1025.     {
  1026.         return '<form name="form_fontsize_selection" id="form_fontsize_selection"'
  1027.             . ' method="post" action="index.php" target="_parent">' . "\n"
  1028.             . PMA_generate_common_hidden_inputs() . "\n"
  1029.             . PMA_Config::getFontsizeSelection() . "\n"
  1030.             . '<noscript>' . "\n"
  1031.             . '<input type="submit" value="' . $GLOBALS['strGo'] . '" />' . "\n"
  1032.             . '</noscript>' . "\n"
  1033.             . '</form>';
  1034.     }
  1035. }
  1036. ?>
  1037.